import * as usersServices from '../services/users';

export default {
    namespace: 'users',
    // state: {
    //     list: [],
    //     total: 0,
    //     page: 1,
    //     pageSize: 10,
    // },
    state: {
        list: [],
        total: 0,
        page: 1,
        pageSize: 10,
    },
    reducers: {
        // setData(state, { payload: { list, total, page } }) {
        //     return { ...state, list, total, page };
        // },
        setData(state, { payload: { list, total, page } }) {
            return { ...state, list, total, page };
        }
    },
    effects: {
        /**
         * 
         * @param {*} select 获取state里面的数据 
         * @param {*} put 更新数据
         * @param {*} call 执行需要的方法
         * @param {*} payload 调用当前方法 传过来的参数 
         */
        // *fetch({ payload: { page } }, { call, put, select }) {
        //     // console.log(page);
        //     const pageSize = yield select(state => state.users.pageSize);
        //     const res = yield call(usersServices.fetch, { page, pageSize });
        //     // console.log(res);
        //     // 更新状态
        //     if (res && res.state === 'success') {
        //         yield put({ type: 'setData', payload: { ...res.data, page } });
        //     } else {
        //         yield put({
        //             type: 'setData',
        //             payload: { data: { list: [], total: 0 } },
        //         });
        //     }
        // },
        *fetch({ payload: { page } }, { call, put, select }) {
            const pageSize = yield select(state => state.users.pageSize)
            const res = yield call(usersServices.fetch, { page, pageSize })
            // 更新状态
            if (res && res.state === 'success') {
                yield put({ type: 'setData', payload: { ...res.data, page } })
            } else {
                yield put({
                    type: 'setData',
                    payload: {
                        list: [],
                        total: 0
                    }
                })
            }
        },
        // *add({ payload }, { call }) {
        //     return yield call(usersServices.add, payload);
        // },
        *add({ payload }, { call }) {
            return yield call(usersServices.add, payload)
        },
        // *edit({ payload: { id, value } }, { call }) {
        //     return yield call(usersServices.edit, id, value);
        // },
        *edit({ payload: { id, value } }, { call }) {
            return yield call(usersServices.edit, id, value)
        },
        // *remove({ payload }, { call }) {
        //     return yield call(usersServices.remove, payload);
        // },
        *remove({ payload }, { call }) {
            return yield call(usersServices.remove, payload)
        }
    },
    // 订阅 进入是自执行
    // subscriptions: {
    //     setup({ dispatch, history }) {
    //         // 首先要判断当前的路径
    //         return history.listen(({ pathname }) => {
    //             if (pathname === '/users') {
    //                 dispatch({ type: 'fetch', payload: { page: 1 } });
    //             }
    //         });
    //     },
    // },
    subscriptions: {
        setup({
            dispatch, history
        }) {
            return history.listen(({ pathname }) => {
                if (pathname === '/users') {
                    dispatch({ type: 'fetch', payload: { page: 1 } })
                }
            })
        }
    }
};
